 #include <cv.h>  
 #include <cxcore.h>  
 #include <highgui.h>  
 #include <iostream> 
 #include "Determinator.h"

 using namespace std;  
   
 void processFrame(IplImage*& f, IplImage* obj);
     const char *windowName1 = "First steps with OpenCV1";  
	 const char * wind = "vasya poet";
  
 int main(int argc, char** argv)   
 {  
  
	 int count = 0;
	 IplImage* fromFile = NULL;
	 char* filename = "1.jpg";
	 char* videofile = "1.avi";	
	 fromFile = cvLoadImage(filename);
		cvSmooth( fromFile, fromFile, CV_GAUSSIAN, 3, 3 );
		CvSize size = cvSize(fromFile->width, fromFile->height);
			IplImage* fGray = cvCreateImage(size,fromFile->depth,1);		
			cvCvtColor(fromFile,fGray,CV_RGB2GRAY); 		
		
		    IplImage* seg = Segmentation::Treshold(fGray,10);

			IplImage* obj = Determinator::Decrease(seg,4);			
			cvShowImage(wind,obj);	
			cvReleaseImage(&seg);
			cvReleaseImage(&fGray);
  cvNamedWindow(wind, CV_WINDOW_AUTOSIZE);

  CvCapture* capture = 0;  
  //capture = cvCreateCameraCapture(0);  
	capture = cvCreateFileCapture(videofile);
  if (!capture)  
  {  
   cout << "Initialization failed" << endl;  
   return EXIT_FAILURE;  
 }  
 
  const char *windowName = "First steps with OpenCV";  
  cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);  
  cvNamedWindow(windowName1, CV_WINDOW_AUTOSIZE);
  while(true)   
  {  
 
	  ++count;
   IplImage* f = cvQueryFrame( capture );

   if(!f)   
    break; 
        cvShowImage(windowName, f);	
		if (count%20 == 0)
		{		
			processFrame(f,obj);
		}

		//cvCvtColor(f,f,CV_HSV2BGR);
   char c = cvWaitKey(40);     
   if (c == 27)

	   break;
  }

  cvReleaseCapture( &capture );  
  cvDestroyWindow(windowName);  
  cvDestroyWindow(windowName1);  
 return 0;  
 } 
  
 
 void processFrame(IplImage*& f, IplImage * obj)  
    {     
			
		int segm =3;
		if (segm==0)
		{
		
		cvSmooth( f, f, CV_GAUSSIAN, 3, 3 );
		CvSize size = cvSize(f->width, f->height);
		IplImage* fGray = cvCreateImage(size,f->depth,1);		
		cvCvtColor(f,fGray,CV_RGB2GRAY);
		MyImage* frame = new MyImage (fGray);
		frame->UpdatePlanes();
		BarGraph* BarGR = new BarGraph(frame->GetPlane());
		int k = 0;
		int* a = BarGR->GetK(k);
		IplImage* fSegments = Segmentation::K_means(a,k,frame);
		MyImage* MyfSeg = new MyImage(fSegments);
		MyfSeg->UpdatePlanes();
		
		delete[] a;


		MyfSeg->GetPlane()->MakeCurves();
		MyfSeg->UpdateFromPlanes();		
		
		cvShowImage(windowName1,fSegments);

		delete frame;
		 delete MyfSeg;

		cvReleaseImage(&fGray);
		cvReleaseImage(&fSegments);
		delete BarGR;
		}
		else if (segm == 1)
		{

			cvSmooth( f, f, CV_GAUSSIAN, 3, 3 );
			CvSize size = cvSize(f->width, f->height);
			IplImage* fGray = cvCreateImage(size,f->depth,1);		
			cvCvtColor(f,fGray,CV_RGB2GRAY);
			cvCanny(fGray,fGray,20,50);
			//MyImage* frame = new MyImage (fGray);
			//frame->UpdatePlanes();
			//BarGraph* BarGR = new BarGraph(frame->GetPlane());
			//int k = 0;
			//int* a = BarGR->GetK(k);
			Determinator::Delete(fGray);
			//IplImage* fGrayLess = Determinator::Decrease(fGray,4);
			cvShowImage(windowName1,fGray);
			//delete frame;
			//delete BarGR;
			cvReleaseImage(&fGray);
			//cvReleaseImage(&fGrayLess);
		}else
		{
			cvSmooth( f, f, CV_GAUSSIAN, 3, 3 );
			CvSize size = cvSize(f->width, f->height);
			IplImage* fGray = cvCreateImage(size,f->depth,1);			
	
			cvCvtColor(f,fGray,CV_RGB2GRAY);			
		    IplImage* fSegments1 = Segmentation::Treshold(fGray,10);			
			
			IplImage* fSegments2 = Determinator::Decrease(fSegments1,4);
			Determinator::Compare(fSegments2,obj);
			int* x;
			int* y;
			int count;


			cvShowImage(windowName1,fSegments2);

			//delete[] x;
			//delete[] y;
 			//delete BarGR;
			///delete fSum;
			cvReleaseImage(&fGray);
			//cvReleaseImage(&fSegments);
			cvReleaseImage(&fSegments1);
			cvReleaseImage(&fSegments2);


		}


	//double avr =(image->GetPlane(2)->GetAvr());
	//image->GetPlane(2)->SetToConst(avr);
   } 